/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.business.rest;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.PricRgstIns;
import me.zhengjie.business.domain.PricRgstTrtappy;
import me.zhengjie.business.domain.vo.PricRgstVo;
import me.zhengjie.business.service.PricRgstInsService;
import me.zhengjie.business.service.dto.PricRgstInsQueryCriteria;
import me.zhengjie.business.service.dto.YbryDto;
import me.zhengjie.business.util.HttpNewMicProvince;
import me.zhengjie.business.util.SmUtilProvince;
import me.zhengjie.config.DebugProperties;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.dto.DeptDto;
import me.zhengjie.nmi.ChsInterfaceConfig;
import me.zhengjie.nmi.ChsUtil;
import me.zhengjie.utils.DateTimeUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author xiahongbo
* @date 2025-01-09
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "长护登记信息管理")
@RequestMapping("/api/pricRgst")
public class PricRgstController {

    private final PricRgstInsService pricRgstInsService;

    private final DeptService deptService;

    private final HttpNewMicProvince httpNewMicProvince;



    @GetMapping(value = "/getPersonnelInfo")
    @Log("人员基本信息获取")
    @ApiOperation("人员基本信息获取")
    public ResponseEntity<Object> getPersonnelInfo(String sfz, String xm){
        if(!DebugProperties.debug) {
            log.info("生产模式");
        } else {
            log.info("开发模式");
            sfz = "220103194008040220";
            xm = "侯淑荣";
        }
        log.info("getPersonnelInfo - sfz:{}, xm:{}", sfz, xm);

        String bcn = DateUtil.now() + (int) ((Math.random() * 9 + 1) * 1000);
        UserDetails userDetails = SecurityUtils.getCurrentUser();
        cn.hutool.json.JSONObject userJson = new cn.hutool.json.JSONObject(new cn.hutool.json.JSONObject(userDetails).get("user"));
        cn.hutool.json.JSONObject deptJson = new cn.hutool.json.JSONObject(userJson.get("dept"));
        log.info("get8501 deptJson:{}", deptJson);

        String date = DateTimeUtil.getDate();

        Long deptId = Long.parseLong(deptJson.getStr("id"));
        DeptDto deptDto = deptService.findById(deptId);

        String input = "{\"data\": {" +
                "\"mdtrt_cert_type\":\"02\"," +
                "\"mdtrt_cert_no\": \""+sfz+"\"," +
                "\"card_sn\": \"\"," +
                "\"begntime\": \""+date+"\"," +
                "\"psn_cert_type\": \"01\"," +
                "\"certno\": \""+sfz+"\"," +
                "\"psn_name\": \""+xm+"\"}}";
        log.info("getPersonnelInfo - input:{} ", input);
        //签名
        String sign = SmUtilProvince.sign(input, deptDto);
        //加密
        String sm4Input = SmUtilProvince.encrypt(input, deptDto);

        String message = "{\"infno\": \"1101\"," +
                "\"msgid\": \"" + deptDto.getDeptCode() + ChsUtil.getMsgId() + "\"," +
                "\"mdtrtarea_admvs\": \"" + deptDto.getProvinceLicense() + "\"," +
                "\"insuplc_admdvs\": \"\"," +
                "\"recer_sys_code\": \"" + ChsInterfaceConfig.recerSysCode001 + "\"," +
                "\"dev_no\": \"\"," +
                "\"dev_safe_info\": \"\"," +
                "\"cainfo\": \"" + sign + "\"," +
                "\"signtype\": \"SM2\"," +
                "\"infver\": \"" + ChsInterfaceConfig.infver + "\"," +
                "\"opter_type\": \"1\"," +
                "\"opter\": \"" + SecurityUtils.getCurrentUserId() + "\"," +
                "\"opter_name\": \"" + SecurityUtils.getCurrentNickName() + "\"," +
                "\"inf_time\": \"" + DateUtil.format(DateUtil.date(), DatePattern.NORM_DATE_PATTERN) + "\"," +
                "\"fixmedins_code\": \"" + deptDto.getDeptCode() + "\"," +
                "\"fixmedins_name\": \"" + deptDto.getDeptName() + "\"," +
                "\"sign_no\": \"" + bcn + "\"," +
                "\"input\": \"" + sm4Input + "\"}";

        String encryptResult = httpNewMicProvince.handle(message, "人员基本信息获取", deptDto);
        log.info("getPersonnelInfo - encryptResult:{} ", encryptResult);
        JSONObject result = JSONObject.parseObject(encryptResult);
        if ("0".equals(result.getString("infcode"))) {
            String decryptionResult = SmUtilProvince.decryption(result.getString("output"), deptDto);
            log.info("getPersonnelInfo - decryptionResult:{} ", decryptionResult);

            List<JSONObject> list2 = new ArrayList<>();
            JSONObject reObject = JSONObject.parseObject(decryptionResult);
            JSONObject baseinfo = reObject.getJSONObject("baseinfo");
            JSONArray insuinfo = reObject.getJSONArray("insuinfo");
            if(insuinfo.size() >=2) {
                throw new BadRequestException("有多个参保单位");
            }
            JSONObject insuinfObj =  insuinfo.getJSONObject(0);
            baseinfo.put("insuplc_admdvs", insuinfObj.getString("insuplc_admdvs"));
            baseinfo.put("insutype", insuinfObj.getString("insutype"));
            baseinfo.put("psn_insu_stas", insuinfObj.getString("psn_insu_stas"));
            baseinfo.put("psn_type", insuinfObj.getString("psn_type"));
            baseinfo.put("cvlserv_flag", insuinfObj.getString("cvlserv_flag"));
            baseinfo.put("psn_insu_date", insuinfObj.getString("psn_insu_date"));
            list2.add(baseinfo);

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("content", list2);
            map.put("totalElements", list2.size());

            return new ResponseEntity<>(map, HttpStatus.OK);
        } else {
            //return AjaxResult.error(result.getString("err_msg"));
            throw new BadRequestException(result.getString("err_msg"));
        }

    }

    @GetMapping(value = "/getRgstInfo")
    @Log("护理机构获取参保人长护待遇基本信息")
    @ApiOperation("护理机构获取参保人长护待遇基本信息")
    public ResponseEntity<Object> getRgstInfo(String sfz, String xm, String rybm, String zjlx, String tcq){

        log.info("getRgstInfo - sfz:{}, xm:{}, rybm:{}, zjlx:{}, tcq:{}", sfz, xm, rybm, zjlx, tcq);

        String bcn = DateUtil.now() + (int) ((Math.random() * 9 + 1) * 1000);
        UserDetails userDetails = SecurityUtils.getCurrentUser();
        cn.hutool.json.JSONObject userJson = new cn.hutool.json.JSONObject(new cn.hutool.json.JSONObject(userDetails).get("user"));
        cn.hutool.json.JSONObject deptJson = new cn.hutool.json.JSONObject(userJson.get("dept"));
        log.info("getRgstInfo deptJson:{}", deptJson);

        Long deptId = Long.parseLong(deptJson.getStr("id"));
        DeptDto deptDto = deptService.findById(deptId);

        String input = "{\"data\": {" +
                "\"cert_type\":\""+zjlx+"\"," +
                "\"certno\":\""+sfz+"\"," +
                "\"psn_name\":\""+xm+"\"," +
                "\"nurscare_org_no\":\""+deptDto.getDeptCode()+"\"," +
                "\"psn_no\":\""+rybm+"\"," +
                "\"admdvs\": \""+deptDto.getProvinceLicense()+"\"}}";
        log.info("getRgstInfo - input:{} ", input);
        //签名
        String sign = SmUtilProvince.sign(input, deptDto);
        //加密
        String sm4Input = SmUtilProvince.encrypt(input, deptDto);

        String message = "{\"infno\": \"8501\"," +
                "\"msgid\": \"" + deptDto.getDeptCode() + ChsUtil.getMsgId() + "\"," +
                "\"mdtrtarea_admvs\": \"" + deptDto.getProvinceLicense() + "\"," +
                "\"insuplc_admdvs\": \""+tcq+"\"," +
                "\"recer_sys_code\": \"" + ChsInterfaceConfig.recerSysCode001 + "\"," +
                "\"dev_no\": \"\"," +
                "\"dev_safe_info\": \"\"," +
                "\"cainfo\": \"" + sign + "\"," +
                "\"signtype\": \"SM2\"," +
                "\"infver\": \"" + ChsInterfaceConfig.infver + "\"," +
                "\"opter_type\": \"1\"," +
                "\"opter\": \"" + SecurityUtils.getCurrentUserId() + "\"," +
                "\"opter_name\": \"" + SecurityUtils.getCurrentNickName() + "\"," +
                "\"inf_time\": \"" + DateUtil.format(DateUtil.date(), DatePattern.NORM_DATE_PATTERN) + "\"," +
                "\"fixmedins_code\": \"" + deptDto.getDeptCode() + "\"," +
                "\"fixmedins_name\": \"" + deptDto.getDeptName() + "\"," +
                "\"sign_no\": \"" + bcn + "\"," +
                "\"input\": \"" + sm4Input + "\"}";

        String encryptResult = httpNewMicProvince.handle(message, "护理机构获取参保人长护待遇基本信息", deptDto);
        log.info("getRgstInfo - encryptResult:{} ", encryptResult);
        JSONObject result = JSONObject.parseObject(encryptResult);
        if ("0".equals(result.getString("infcode"))) {
            String decryptionResult = SmUtilProvince.decryption(result.getString("output"), deptDto);

            log.info("getRgstInfo - decryptionResult:{} ", decryptionResult);
            JSONObject reObject = JSONObject.parseObject(decryptionResult);
            return new ResponseEntity<>(reObject, HttpStatus.OK);
        } else {
            //return AjaxResult.error(result.getString("err_msg"));
            throw new BadRequestException(result.getString("err_msg"));
        }

    }

    @GetMapping(value = "/getBanks")
    @Log("银行信息查询")
    @ApiOperation("银行信息查询")
    public ResponseEntity<Object> getBanks(String bank_type_code, String bank_abbr){

        String bcn = DateUtil.now() + (int) ((Math.random() * 9 + 1) * 1000);
        UserDetails userDetails = SecurityUtils.getCurrentUser();
        cn.hutool.json.JSONObject userJson = new cn.hutool.json.JSONObject(new cn.hutool.json.JSONObject(userDetails).get("user"));
        cn.hutool.json.JSONObject deptJson = new cn.hutool.json.JSONObject(userJson.get("dept"));
        log.info("getBanks deptJson:{}", deptJson);

        String date = DateTimeUtil.getDate();

        Long deptId = Long.parseLong(deptJson.getStr("id"));
        DeptDto deptDto = deptService.findById(deptId);

        String input = "{\"data\": {" +
                "\"bank_abbr\":\""+bank_abbr+"\"," +
                "\"bank_type_code\": \""+bank_type_code+"\"}}";
        log.info("getBanks - input:{} ", input);
        //签名
        String sign = SmUtilProvince.sign(input, deptDto);
        //加密
        String sm4Input = SmUtilProvince.encrypt(input, deptDto);

        String message = "{\"infno\": \"8610\"," +
                "\"msgid\": \"" + deptDto.getDeptCode() + ChsUtil.getMsgId() + "\"," +
                "\"mdtrtarea_admvs\": \"" + deptDto.getProvinceLicense() + "\"," +
                "\"insuplc_admdvs\": \"\"," +
                "\"recer_sys_code\": \"" + ChsInterfaceConfig.recerSysCode001 + "\"," +
                "\"dev_no\": \"\"," +
                "\"dev_safe_info\": \"\"," +
                "\"cainfo\": \"" + sign + "\"," +
                "\"signtype\": \"SM2\"," +
                "\"infver\": \"" + ChsInterfaceConfig.infver + "\"," +
                "\"opter_type\": \"1\"," +
                "\"opter\": \"" + SecurityUtils.getCurrentUserId() + "\"," +
                "\"opter_name\": \"" + SecurityUtils.getCurrentNickName() + "\"," +
                "\"inf_time\": \"" + DateUtil.format(DateUtil.date(), DatePattern.NORM_DATE_PATTERN) + "\"," +
                "\"fixmedins_code\": \"" + deptDto.getDeptCode() + "\"," +
                "\"fixmedins_name\": \"" + deptDto.getDeptName() + "\"," +
                "\"sign_no\": \"" + bcn + "\"," +
                "\"input\": \"" + sm4Input + "\"}";

        String encryptResult = httpNewMicProvince.handle(message, "银行信息查询", deptDto);
        log.info("getBanks - encryptResult:{} ", encryptResult);
        JSONObject result = JSONObject.parseObject(encryptResult);
        if ("0".equals(result.getString("infcode"))) {
            String decryptionResult = SmUtilProvince.decryption(result.getString("output"), deptDto);
            log.info("getBanks - decryptionResult:{} ", decryptionResult);

            JSONArray array = JSONArray.parseArray(decryptionResult);
            Map<String, Object> map = new HashMap();
            map.put("content", array);
            map.put("totalElements", array.size());

            return new ResponseEntity<>(map, HttpStatus.OK);
        } else {
            //return AjaxResult.error(result.getString("err_msg"));
            throw new BadRequestException(result.getString("err_msg"));
        }

    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('pricRgst:list')")
    public void exportPricRgstIns(HttpServletResponse response, PricRgstInsQueryCriteria criteria) throws IOException {
        pricRgstInsService.download(pricRgstInsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询长护登记信息")
    @ApiOperation("查询长护登记信息")
    @PreAuthorize("@el.check('pricRgst:list')")
    public ResponseEntity<Object> queryPricRgstIns(PricRgstInsQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(pricRgstInsService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增长护登记信息")
    @ApiOperation("新增长护登记信息")
    @PreAuthorize("@el.check('pricRgst:add')")
    public ResponseEntity<Object> createPricRgst(@Validated @RequestBody PricRgstVo rgstVo){

        log.info("createPricRgst PricRgstVo:{} ", rgstVo);


        String bcn = DateTimeUtil.getDateTime("yyyyMMddHHmmss")+ (int) ((Math.random() * 9 + 1) * 1000);
        UserDetails userDetails = SecurityUtils.getCurrentUser();
        cn.hutool.json.JSONObject userJson = new cn.hutool.json.JSONObject(new cn.hutool.json.JSONObject(userDetails).get("user"));
        cn.hutool.json.JSONObject deptJson = new cn.hutool.json.JSONObject(userJson.get("dept"));

        String date = DateTimeUtil.getDate();

        Long deptId = Long.parseLong(deptJson.getStr("id"));
        DeptDto deptDto = deptService.findById(deptId);

        PricRgstIns resources = new PricRgstIns();
        resources.setPricRgstInsId(IdUtil.simpleUUID());
        resources.setLongtermRegId(DateTimeUtil.getDateTime("yyyyMMddHHmmss"));
        resources.setLongtermTrtAppyId(rgstVo.getLongtermTrtAppyId());
        resources.setLongtermEvalTaskId(rgstVo.getLongtermEvalTaskId());
        resources.setPsnNo(rgstVo.getPsnNo());
        resources.setPsnName(rgstVo.getPsnName());
        resources.setGend(rgstVo.getGend());
        resources.setCertType(rgstVo.getCertType());
        resources.setCertno(rgstVo.getCertno());
        resources.setBrdy(rgstVo.getBrdy());
        resources.setInsutype(rgstVo.getInsutype());
        resources.setInsuAdmdvs(rgstVo.getInsuAdmdvs());
        resources.setLongtermEvalLv(rgstVo.getLongtermEvalLv());
        resources.setNurscareOrgNo(deptDto.getDeptCode());
        resources.setNurscareOrgName(deptDto.getDeptName());
        resources.setNurscareWay("01");
        resources.setBegntime(rgstVo.getBegntime());
        resources.setLongtermOrgNurscareRegnId(rgstVo.getLongtermOrgNurscareRegnId());
        resources.setLongtermOrgBedId(rgstVo.getLongtermOrgBedId());
        resources.setBankTypeCode(rgstVo.getBankTypeCode());
        resources.setBankId(rgstVo.getBankId());
        resources.setBankName(rgstVo.getBankName());
        resources.setBankacct(rgstVo.getBankacct());
        resources.setBankAcctname(rgstVo.getBankAcctname());
        resources.setOutLiveFlag(rgstVo.getOutLiveFlag());
        resources.setNurscareRegn(rgstVo.getNurscareRegn());
        resources.setBedno(rgstVo.getBedno());
        resources.setSupMedstffName(rgstVo.getSupMedstffName());
        resources.setSupMedstffNo(rgstVo.getSupMedstffNo());
        resources.setTrtFlag(rgstVo.getTrtFlag());


        PricRgstTrtappy appy = new PricRgstTrtappy();
        appy.setLongtermTrtAppyId(IdUtil.simpleUUID());
        appy.setLongtermTrtAppyId(rgstVo.getLongtermTrtAppyId());
        appy.setPsnNo(rgstVo.getPsnNo());
        appy.setPsnName(rgstVo.getPsnName());
        appy.setGend(rgstVo.getGend());
        appy.setCertType(rgstVo.getCertType());
        appy.setCertno(rgstVo.getCertno());
        appy.setBrdy(rgstVo.getBrdy());
        appy.setNaty(rgstVo.getNaty());
        appy.setInsutype(rgstVo.getInsutype());
        appy.setInsuAdmdvs(rgstVo.getInsuAdmdvs());
        appy.setLiveLocAdmdvs(rgstVo.getLiveLocAdmdvs());
        appy.setLiveAddr(rgstVo.getLiveAddr());
        appy.setInsuIdet(rgstVo.getInsuIdet());
        appy.setTel(rgstVo.getTel());
        appy.setFstAppyFlag(rgstVo.getFstAppyFlag());
        appy.setLongtermEvalWay(rgstVo.getLongtermEvalWay());
        appy.setEvalLocAdmdvs(rgstVo.getEvalLocAdmdvs());
        appy.setEvalAddr(rgstVo.getEvalAddr());
        appy.setNurscareWay("01");
        appy.setAgnterCertno(rgstVo.getAgnterCertno());
        appy.setAgnterCertType(rgstVo.getAgnterCertType());
        appy.setAgnterGend(rgstVo.getAgnterGend());
        appy.setAgnterLiveAddr(rgstVo.getAgnterLiveAddr());
        appy.setAgnterLiveLocAdmdvs(rgstVo.getAgnterLiveLocAdmdvs());
        appy.setAgnterName(rgstVo.getAgnterName());
        appy.setAgnterRlts(rgstVo.getAgnterRlts());
        appy.setAgnterTel(rgstVo.getAgnterTel());
        appy.setNatRegnCode(rgstVo.getNatRegnCode());
        appy.setResdAddr(rgstVo.getResdAddr());
        appy.setResdLocAdmdvs(rgstVo.getResdLocAdmdvs());
        appy.setLastAppyFlag(rgstVo.getLastAppyFlag());
        appy.setNurscareOrgLocAdmdvs(rgstVo.getNurscareOrgLocAdmdvs());
        appy.setNurscareOrgAddr(rgstVo.getNurscareOrgAddr());
        appy.setServLocAdmdvs(rgstVo.getServLocAdmdvs());
        appy.setServPath(rgstVo.getServPath());
        appy.setCrtNurscareWay(rgstVo.getCrtNurscareWay());
        appy.setAcpStas(rgstVo.getAcpStas());
        appy.setTrtAcpOpnn(rgstVo.getTrtAcpOpnn());
        appy.setUndoReaCodg(rgstVo.getUndoReaCodg());
        appy.setUndoReaDscr(rgstVo.getUndoReaDscr());
        appy.setTrtAppySouc(rgstVo.getTrtAppySouc());
        appy.setInfoSbmtTime(rgstVo.getInfoSbmtTime());
        appy.setAppyDate(rgstVo.getAppyDate());
        appy.setEvalCclsValiEnddate(rgstVo.getEvalCclsValiEnddate());
        appy.setSelfEvalLv(rgstVo.getSelfEvalLv());
        appy.setScaleAnsrRsltId(rgstVo.getScaleAnsrRsltId());
        appy.setLongtermAgnterId(rgstVo.getLongtermAgnterId());
        appy.setBindFlag(rgstVo.getBindFlag());
        appy.setOptDotId(rgstVo.getOptDotId());
        appy.setDataVer(rgstVo.getDataVer());
        appy.setLnt(rgstVo.getLnt());
        appy.setLat(rgstVo.getLat());




        String input = "{\"trtappy\": {" +
                "\"longterm_trt_appy_id\":\""+appy.getLongtermTrtAppyId()+"\"," +
                "\"psn_no\":\""+appy.getPsnNo()+"\"," +
                "\"psn_name\":\""+appy.getPsnName()+"\"," +
                "\"gend\":\""+appy.getGend()+"\"," +
                "\"cert_type\":\""+appy.getCertType()+"\"," +
                "\"certno\":\""+appy.getCertno()+"\"," +
                "\"brdy\":\""+DateUtil.format(appy.getBrdy(), DatePattern.NORM_DATE_PATTERN)+"\"," +
                "\"naty\":\""+appy.getNaty()+"\"," +
                "\"insu_admdvs\":\""+resources.getInsuAdmdvs()+"\"," +
                "\"admdvs\":\""+appy.getAdmdvs()+"\"," +
                "\"nat_regn_code\":\""+appy.getNatRegnCode()+"\"," +
                "\"resd_loc_admdvs\":\""+appy.getResdLocAdmdvs()+"\"," +
                "\"resd_addr\":\""+appy.getResdAddr()+"\"," +
                "\"live_loc_admdvs\":\""+appy.getLiveLocAdmdvs()+"\"," +
                "\"live_addr\":\""+appy.getLiveAddr()+"\"," +
                "\"insu_idet\":\""+appy.getInsuIdet()+"\"," +
                "\"tel\":\""+appy.getTel()+"\"," +
                "\"lnt\":\""+appy.getLnt()+"\"," +
                "\"lat\":\""+appy.getLat()+"\"," +
                "\"fst_appy_flag\":\""+appy.getFstAppyFlag()+"\"," +
                "\"last_appy_flag\":\""+appy.getLastAppyFlag()+"\"," +
                "\"longterm_eval_way\":\""+appy.getLongtermEvalWay()+"\"," +
                "\"eval_loc_admdvs\":\""+appy.getEvalLocAdmdvs()+"\"," +
                "\"eval_addr\":\""+appy.getEvalAddr()+"\"," +
                "\"nurscare_way\":\""+appy.getNurscareWay()+"\"," +
                "\"nurscare_org_no\":\""+appy.getNurscareOrgNo()+"\"," +
                "\"nurscare_org_name\":\""+appy.getNurscareOrgName()+"\"," +
                "\"nurscare_org_loc_admdvs\":\""+appy.getNurscareOrgLocAdmdvs()+"\"," +
                "\"nurscare_org_addr\":\""+appy.getNurscareOrgAddr()+"\"," +
                "\"serv_loc_admdvs\":\""+appy.getServLocAdmdvs()+"\"," +
                "\"serv_path\":\""+appy.getServPath()+"\"," +
                "\"crt_nurscare_wa\":\""+appy.getCrtNurscareWay()+"\"," +
                "\"dise_dscr\":\""+appy.getDiseDscr()+"\"," +
                "\"insutype\":\""+appy.getInsutype()+"\"," +
                "\"acp_stas\":\""+appy.getAcpStas()+"\"," +
                "\"trt_acp_opnn\":\""+appy.getTrtAcpOpnn()+"\"," +
                "\"undo_rea_codg\":\""+appy.getUndoReaCodg()+"\"," +
                "\"undo_rea_dscr\":\""+appy.getUndoReaDscr()+"\"," +
                "\"trt_appy_souc\":\""+appy.getTrtAppySouc()+"\"," +
                "\"info_sbmt_time\":\""+DateUtil.format(appy.getInfoSbmtTime(), DatePattern.NORM_DATETIME_PATTERN)+"\"," +
                "\"appy_date\":\""+DateUtil.format(appy.getAppyDate(), DatePattern.NORM_DATE_PATTERN)+"\"," +
                "\"eval_ccls_vali_enddate\":\""+DateUtil.format(appy.getEvalCclsValiEnddate(), DatePattern.NORM_DATE_PATTERN)+"\"," +
                "\"self_eval_lv\":\""+appy.getSelfEvalLv()+"\"," +
                "\"scale_ansr_rslt_id\":\""+appy.getScaleAnsrRsltId()+"\"," +
                "\"longterm_agnter_id\":\""+appy.getLongtermAgnterId()+"\"," +
                "\"bind_flag\":\""+appy.getBindFlag()+"\"," +
                "\"opt_dot_id\":\""+appy.getOptDotId()+"\"," +
                "\"data_ver\":\""+appy.getDataVer()+"\"," +
                "\"agnter_name\":\""+appy.getAgnterName()+"\"," +
                "\"agnter_cert_type\":\""+appy.getAgnterCertType()+"\"," +
                "\"agnter_certno\":\""+appy.getAgnterCertno()+"\"," +
                "\"agnter_rlts\":\""+appy.getAgnterRlts()+"\"," +
                "\"agnter_gend\":\""+appy.getAgnterGend()+"\"," +
                "\"agnter_tel\":\""+appy.getAgnterTel()+"\"," +
                "\"agnter_live_loc_admdvs\":\""+appy.getAgnterLiveLocAdmdvs()+"\"," +
                "\"agnter_live_addr\": \""+appy.getAgnterLiveAddr()+"\"}," +
                "\"ins\": {" +
                "\"longterm_reg_id\":\""+resources.getLongtermRegId()+"\"," +
                "\"longterm_trt_appy_id\":\""+resources.getLongtermTrtAppyId()+"\"," +
                "\"longterm_eval_task_id\":\""+resources.getLongtermEvalTaskId()+"\"," +
                "\"psn_no\":\""+resources.getPsnNo()+"\"," +
                "\"psn_name\":\""+resources.getPsnName()+"\"," +
                "\"gend\":\""+resources.getGend()+"\"," +
                "\"cert_type\":\""+resources.getCertType()+"\"," +
                "\"certno\":\""+resources.getCertno()+"\"," +
                "\"brdy\":\""+DateUtil.format(resources.getBrdy(), DatePattern.NORM_DATE_PATTERN)+"\"," +
                "\"insutype\":\""+resources.getInsutype()+"\"," +
                "\"insu_admdvs\":\""+resources.getInsuAdmdvs()+"\"," +
                "\"longterm_eval_lv\":\""+resources.getLongtermEvalLv()+"\"," +
                "\"nurscare_org_no\":\""+resources.getNurscareOrgNo()+"\"," +
                "\"nurscare_org_name\":\""+resources.getNurscareOrgName()+"\"," +
                "\"sup_medstff_no\":\""+resources.getSupMedstffNo()+"\"," +
                "\"sup_medstff_name\":\""+resources.getSupMedstffName()+"\"," +
                "\"nurscare_way\":\""+resources.getNurscareWay()+"\"," +
                "\"trt_flag\":\""+resources.getTrtFlag()+"\"," +
                "\"begntime\":\""+DateUtil.format(resources.getBegntime(), DatePattern.NORM_DATETIME_PATTERN)+"\"," +
                "\"disecode\":\""+resources.getDisecode()+"\"," +
                "\"disename\":\""+resources.getDisename()+"\"," +
                "\"dept_name\":\""+resources.getDeptName()+"\"," +
                "\"nurscare_regn\":\""+resources.getNurscareRegn()+"\"," +
                "\"bedno\":\""+resources.getBedno()+"\"," +
                "\"year\":\""+resources.getYear()+"\"," +
                "\"fix_blng_admdvs\":\""+resources.getFixBlngAdmdvs()+"\"," +
                "\"longterm_org_nurscare_regn_id\":\""+resources.getLongtermOrgNurscareRegnId()+"\"," +
                "\"longterm_org_bed_id\":\""+resources.getLongtermOrgBedId()+"\"," +
                "\"rels_medstff_no\":\""+resources.getRelsMedstffNo()+"\"," +
                "\"rels_medstff_name\":\""+resources.getRelsMedstffName()+"\"," +
                "\"bank_type_code\":\""+resources.getBankTypeCode()+"\"," +
                "\"bank_id\":\""+resources.getBankId()+"\"," +
                "\"bank_name\":\""+resources.getBankName()+"\"," +
                "\"bankacct\":\""+resources.getBankacct()+"\"," +
                "\"bank_acctname\":\""+resources.getBankAcctname()+"\"," +
                "\"out_live_flag\": \""+resources.getOutLiveFlag()+"\"}," +
                "\"att\": []" +
                "}";


        log.info("createPricRgst - input:{}", input);
        //签名
        String sign = SmUtilProvince.sign(input, deptDto);
        //加密
        String sm4Input = SmUtilProvince.encrypt(input, deptDto);

        String message = "{\"infno\": \"8502\"," +
                "\"msgid\": \"" + deptDto.getDeptCode() + ChsUtil.getMsgId() + "\"," +
                "\"mdtrtarea_admvs\": \"" + deptDto.getProvinceLicense() + "\"," +
                "\"insuplc_admdvs\": \""+appy.getAdmdvs()+"\"," +
                "\"recer_sys_code\": \"" + ChsInterfaceConfig.recerSysCode001 + "\"," +
                "\"dev_no\": \"\"," +
                "\"dev_safe_info\": \"\"," +
                "\"cainfo\": \"" + sign + "\"," +
                "\"signtype\": \"SM2\"," +
                "\"infver\": \"" + ChsInterfaceConfig.infver + "\"," +
                "\"opter_type\": \"1\"," +
                "\"opter\": \"" + SecurityUtils.getCurrentUserId() + "\"," +
                "\"opter_name\": \"" + SecurityUtils.getCurrentNickName() + "\"," +
                "\"inf_time\": \"" + DateUtil.format(DateUtil.date(), DatePattern.NORM_DATE_PATTERN) + "\"," +
                "\"fixmedins_code\": \"" + deptDto.getDeptCode() + "\"," +
                "\"fixmedins_name\": \"" + deptDto.getDeptName() + "\"," +
                "\"sign_no\": \"" + bcn + "\"," +
                "\"input\": \"" + sm4Input + "\"}";
        log.info("createPricRgst - message:{}", message);

        String encryptResult = httpNewMicProvince.handle(message, "护理机构长护待遇登记", deptDto);
        log.info("createPricRgst - 护理机构长护待遇登记:{}", encryptResult);
        JSONObject result = JSONObject.parseObject(encryptResult);
        if ("0".equals(result.getString("infcode"))) {
            String decryptionResult = SmUtilProvince.decryption(result.getString("output"), deptDto);

            log.info("createPricRgst - decryptionResult:{}", decryptionResult);
            JSONObject object = JSONObject.parseObject(decryptionResult);
            resources.setLongtermRegId(object.getString("longterm_regid"));
            pricRgstInsService.create(resources, appy);
            return new ResponseEntity<>(HttpStatus.OK);
        } else {
            //return AjaxResult.error(result.getString("err_msg"));
            throw new BadRequestException(result.getString("err_msg"));
        }

/*
        PricRgstIns resources = new PricRgstIns();
        return new ResponseEntity<>(pricRgstInsService.create(resources),HttpStatus.CREATED);*/
    }

    @PutMapping
    @Log("修改长护登记信息")
    @ApiOperation("修改长护登记信息")
    @PreAuthorize("@el.check('pricRgst:edit')")
    public ResponseEntity<Object> updatePricRgstIns(@Validated @RequestBody PricRgstIns resources){
        pricRgstInsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除长护登记信息")
    @ApiOperation("删除长护登记信息")
    @PreAuthorize("@el.check('pricRgst:del')")
    public ResponseEntity<Object> deletePricRgstIns(@RequestBody String[] ids) {
        pricRgstInsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}